package com.sneakxy.cloudbase.platform.bean.dto;

import java.util.ArrayList;
import java.util.List;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.springframework.beans.BeanUtils;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.sneakxy.cloudbase.platform.bean.Dict;
import com.sneakxy.cloudbase.platform.bean.ValidAdd;
import com.sneakxy.cloudbase.platform.bean.ValidEdit;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 数据字典信息DTO
 * @author 潜行的虚影
 * @see
 */
@Data
@ApiModel("数据字典信息")
@EqualsAndHashCode
@JsonIgnoreProperties(ignoreUnknown = true)
public class DictDTO implements java.io.Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7864877518512312800L;

	public static final String FIELD_NAME_VALUE = "value";
	
	public static final String FIELD_NAME_PARENT_ID = "parentId";

	@NotBlank(groups={ValidEdit.class})
	@Size(max=32, groups={ValidEdit.class})
	@ApiModelProperty("字典ID")
	private String id;

	@NotBlank(groups = {ValidAdd.class, ValidEdit.class})
	@Size(max=50, groups={ValidAdd.class, ValidEdit.class})
	@ApiModelProperty("字典标签")
	private String label;

	@Size(max=100, groups={ValidAdd.class, ValidEdit.class})
	@ApiModelProperty("备注")
	private String memo;

	@ApiModelProperty("上级ID")
	private String parentId;

	@NotNull(groups={ValidAdd.class, ValidEdit.class})
	@ApiModelProperty("排序")
	private Integer sort;

	@NotBlank(groups = {ValidAdd.class, ValidEdit.class})
	@Size(max=100, groups={ValidAdd.class, ValidEdit.class})
	@ApiModelProperty("字典值")
	private String value;

	@ApiModelProperty("字典ID")
	private String dictId;

	@ApiModelProperty("层级")
	private Integer level;
	
	@ApiModelProperty("下级数据字典")
	private List<DictDTO> children;

	public static DictDTO from(Dict entity) {
		DictDTO result = new DictDTO();
		BeanUtils.copyProperties(entity, result);
		return result;
	}
	
	public static List<DictDTO> from(Iterable<Dict> list) {
		List<DictDTO> result = new ArrayList<>();
		list.forEach(entity -> {
			result.add(from(entity));
		});
		return result;
	}
	
	public static Dict to(DictDTO entity) {
		Dict result = new Dict();
		BeanUtils.copyProperties(entity, result);
		return result;
	}
	
	public static List<Dict> to(List<DictDTO> list) {
		List<Dict> result = new ArrayList<>(list.size());
		list.forEach(entity -> {
			result.add(to(entity));
		});
		return result;
	}

}
